<?php
/**
 * Created by PhpStorm.
 * User: li gui gong
 * Date: 2017/4/26
 * Time: 20:10
 */
namespace Home\Model;
use Think\Model;
class AdminModel extends Model{
    public function search($pageSize = 5)
    {
        /**************************************** 搜索 ****************************************/
        $where = array();
        if($realname = I('get.realname'))
            $where['realname'] = array('like', "%$realname%");

        /************************************* 翻页 ****************************************/
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        // 修改分页样式
        $page->setConfig('header', '共%TOTAL_ROW%条数据，当前第%NOW_PAGE%/%TOTAL_PAGE%页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('first', '首页');
        $page->setConfig('last',   '尾页');
        $page->setConfig('next', '下一页');
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE%  %LINK_PAGE%  %DOWN_PAGE% %END%');
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/
        //select a.*,GROUP_CONCAT(c.role_name) from php34_admin a left join bk_admin_role b on a.id=b.admin_id left join bk_role c on c.id=b.role_id group by a.id;
        $data['data'] = $this->field('a.*,GROUP_CONCAT(c.role_name) role_name')->alias('a')->where($where)->join('left join zn_admin_role b on a.id=b.admin_id left join zn_role c on c.id=b.role_id')->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
        return $data;
    }

//    // 添加前
//    protected function _before_insert(&$data, $option)
//    {
//        $data['password'] = md5($data['password'].C('MD5_KEY'));
//    }
    // 添加后,在管理员添加后,在管理员和权限表中,增加管理员所属的权限(因为此时已经产生admin的id)
    protected function _after_insert($data, $option)
    {
        $roleId = I('post.role_id');
        if($roleId){//如果选择了角色的话
            $arModel = M('AdminRole');
            foreach($roleId as $k=>$v){
                $arModel->add(array(
                    'role_id' => $v,
                    'admin_id' => $data['id'],
                ));
            }
        }
    }

    // 修改前
    protected function _before_update(&$data, $option)
    {
        //编辑也不能将超级管理员禁用
        if($option['where']['id'] == 1){
            $data['is_use'] = 1;//默认给他设置成一
        }
        //更新之前要把之前的角色id删除
        //在修改前将他的所有的角色id都清除
        $arModel = M('AdminRole');
        $arModel->where(array('admin_id'=>array('eq',$option['where']['id'])))->delete();
        //接收表单重新添加一次
        $priId = I('post.role_id');
        if($priId){
            foreach($priId as $k=>$v){
                $arModel->add(array(
                    'role_id' => $v,
                    'admin_id' => $option['where']['id'],
                ));
            }
        }
    }
    // 删除前
    protected function _before_delete($option)
    {
        //如果是超级管理员不允许删除
        if($option['where']['id']==1){
            return show(0,'超级管理员不能被删除!');
        }
        //在删除admin中管理员的信息之前先删除admin_role表中管理员对应的数据
        $arModel = M('AdminRole');
        $arModel->where(array('admin_id'=>array('eq',$option['where']['id'])))->delete();
    }

}

















